Passed
Branch master (2105e3)
by Rafael S.
01:29
created

errors.js ➔ describe(ꞌerrorsꞌ)   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 117

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 117
rs 8.2857

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
/*!
2
 * Copyright (c) 2017 Rafael da Silva Rocha.
3
 * 
4
 */
5
6
let chai = require("chai");
7
let expect = chai.expect;
8
9
describe('errors', function() {
10
    
11
    let fs = require("fs");
12
    let wavefile = require("../index.js");
13
    let path = "test/files/";
14
15
    let testFunc;
16
    const unsupportedFormatError = "Not a supported format.";
17
    const noWAVEChunkError = "Could not find the 'WAVE' chunk";
18
    const noFmtChunkError = "Could not find the 'fmt ' chunk";
19
    const noDataChunkError = "Could not find the 'data' chunk";
20
    const sampleRateError = "Invalid sample rate.";
21
    const numChannelsError = "Invalid number of channels.";
22
        
23
    it("should throw an error if not a RIFF file",
24
            function () {
25
        testFunc = function() {
26
            let wBytes = fs.readFileSync(path + "RF64-64-bit-8kHz--mono-bext.wav");
27
            let wav = new wavefile.WaveFile();
28
            wav.fromBytes(wBytes);
29
        };
30
        expect(testFunc).to.throw(unsupportedFormatError);
31
    });
32
33
    it("should throw an error if there is no 'WAVE' chunk",
34
            function () {
35
        testFunc = function() {
36
            let wBytes = fs.readFileSync(path + "16-bit-8kHz-noBext-mono.wav");
37
            wBytes[10] = 0;
38
            let wav = new wavefile.WaveFile();
39
            wav.fromBytes(wBytes);
40
41
        };
42
        expect(testFunc).to.throw(noWAVEChunkError);
43
    });
44
45
    it("should throw an error if there is no 'fmt ' chunk",
46
            function () {
47
        testFunc = function() {
48
            let wBytes = fs.readFileSync(path + "16-bit-8kHz-noBext-mono.wav");
49
            wBytes[14] = 0;
50
            let wav = new wavefile.WaveFile();
51
            wav.fromBytes(wBytes);
52
        };
53
        expect(testFunc).to.throw(noFmtChunkError);
54
    });
55
56
    it("should throw an error if there is no 'data' chunk",
57
            function () {
58
        testFunc = function() {
59
            let wBytes = fs.readFileSync(path + "32bit-48kHz-noBext-mono.wav");
60
            wBytes[36] = 0;
61
            let wav = new wavefile.WaveFile(wBytes);
62
        };
63
        expect(testFunc).to.throw(noDataChunkError);
64
    });
65
66
    it("should throw an error if the sample rate is < 1",
67
            function () {
68
        testFunc = function() {
69
            let wBytes = fs.readFileSync(path + "32bit-48kHz-noBext-mono.wav");
70
            let wav = new wavefile.WaveFile();
71
            wav.fromBytes(wBytes);
72
            wav.sampleRate = 0;
73
            wav.toBytes();
74
        };
75
        expect(testFunc).to.throw(sampleRateError);
76
    });
77
78
    it("should throw an error if the number of channels is < 1",
79
            function () {
80
        testFunc = function() {
81
            let wBytes = fs.readFileSync(path + "32bit-48kHz-noBext-mono.wav");
82
            let wav = new wavefile.WaveFile();
83
            wav.fromBytes(wBytes);
84
            wav.numChannels = 0;
85
            wav.toBytes();
86
        };
87
        expect(testFunc).to.throw(numChannelsError);
88
    });
89
90
    it("should throw an error if enforceFact is true and there is not 'fact' chunk",
91
            function () {
92
        testFunc = function() {
93
            let wBytes = fs.readFileSync(path + "32bit-48kHz-noBext-mono.wav");
94
            let wav = new wavefile.WaveFile();
95
            wav.enforceFact = true;
96
            wav.fromBytes(wBytes);
97
            wav.toBytes();
98
        };
99
        expect(testFunc).to.throw("Could not find the 'fact' chunk");
100
    });
101
102
    it("should throw an error if enforceBext is true and there is not 'bext' chunk",
103
            function () {
104
        testFunc = function() {
105
            let wBytes = fs.readFileSync(path + "32bit-48kHz-noBext-mono.wav");
106
            let wav = new wavefile.WaveFile();
107
            wav.enforceBext = true;
108
            wav.fromBytes(wBytes);
109
            wav.toBytes();
110
        };
111
        expect(testFunc).to.throw("Could not find the 'bext' chunk");
112
    });
113
114
    it("should throw an error if the bit depth is not valid",
115
            function () {
116
        testFunc = function() {
117
            let wBytes = fs.readFileSync(path + "32bit-48kHz-noBext-mono.wav");
118
            let wav = new wavefile.WaveFile();
119
            wav.fromBytes(wBytes);
120
            wav.bitDepth_ = 3;
121
            wav.toBytes();
122
        };
123
        expect(testFunc).to.throw("Invalid bit depth.");
124
    });
125
});
126